package com.xbjtech.service;

import java.sql.SQLException;
import java.util.List;

import org.springframework.stereotype.Service;
import org.apache.ibatis.session.SqlSessionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xbjtech.mapper.OrderMapper;
import com.xbjtech.model.Order;
import com.xbjtech.model.OrderExample;
import com.xbjtech.util.AppConfig;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
	@Autowired
	OrderMapper orderMapper;
	
	@Override
	public PageInfo<Order> selectOrderListPage(OrderExample example, Integer currentPage) {
        PageHelper.startPage(currentPage, AppConfig.PAGESIZE);
        List<Order> list = orderMapper.selectByExample(example);
        return new PageInfo<Order>(list);
	}

	@Override
	public void insertOrder(Order order) throws SQLException {
		orderMapper.insert(order);
	}

	@Override
	public void deleteById(Long id) throws SQLException {
		Order order = new Order();
		order.setId(id);
		order.setDeleted(1);
		orderMapper.updateByPrimaryKeySelective(order);
	}

	@Override
	public Order selectById(Long id) {
		return orderMapper.selectByPrimaryKey(id);
	}

	@Override
	public void merge(Order order) throws SqlSessionException {
		if (null == order.getId()) {
			orderMapper.insert(order);
		} else if (null != order.getId()) {
			orderMapper.updateByPrimaryKeyWithBLOBs(order);
		}
	}
}
